<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Calendar extends CI_Controller
{
	function __construct()
	{
		parent::__construct();		
		if (!$this->tank_auth->is_logged_in()) redirect('auth/login');
				
		//load models
		$this->load->model('employees/Employees_db');
		$this->load->model('employee_leaves/Employee_Leaves_db');
		$this->load->model('punches/Punches_db');
		$this->load->model('punch_alterations/Punch_alterations_db');
		$this->load->model('leave_applications/Leave_applications_db');
		$this->load->model('holidays/Holidays_db');
	}

	function index()
	{			
		//default
		$data['ptitle'] = "Meinhardt - Calendar";
		$data['username'] = $this->tank_auth->get_username();
		$data['module'] = 'calendar';
		$data['active_header'] = 'timekeeping';
		//end default
		
		//dropdown
		$data['leaves'] = $this->Employee_Leaves_db->getEmployee_Leaves(array('user_id'=>$this->tank_auth->get_user_id()));
		
		$data['user'] = $this->Employees_db->getEmployeeMoreDetails(array('a.id'=>$this->tank_auth->get_user_id()));
		$data['content'] = $this->load->view('index',$data,true);		
		$this->load->view('default_view',$data);
	}
	function getCurrentTime()
	{		
		$setdate = $this->input->post('setdate');
		if($setdate)
		{
			$now = date('m',strtotime('now')).' '.date('Y',strtotime('now'));
			$new = date('m',strtotime($setdate)).' '.date('Y',strtotime($setdate));
			
			if($now==$new)
				echo json_encode(array('datetime'=>'equal'));
			else
				echo json_encode(array('datetime'=>strtotime($setdate)));
		}
		else
			echo json_encode(array('datetime'=>strtotime('now')));
	}
	
	function getCalendarEvents($userid = false)
	{
		$start = $this->input->post('start');
		$end = $this->input->post('end');
		$start = gmdate("Y-m-d H:i:s", $start);
		$end = gmdate("Y-m-d H:i:s", $end);
		
		$data = array();
		$dt = null;
		
		
		//get holidays
		$holidays = $this->Holidays_db->getHolidays("holiday_month >= MONTH('$start') AND holiday_month <= MONTH('$end')");
		foreach($holidays as $h)
		{
			$dt = array(
				'title' => 'Holiday - '.$h['holiday_name'],
				'start' => mktime(0, 0, 0, $h['holiday_month'], $h['holiday_day'], date("y",strtotime('now'))),
				'allDay' => true,
				'editable' => false,
				'disableDragging' => true,
				'disableResizing' => true,
				'addClass' => 'holiday',
				'eventID' => $h['holiday_id'],
				'type' => 'holiday',
				'isHidden' => false
				);
			if($dt)
				array_push($data, $dt);
		}
		
		
		//get birthdays
		$birthday = $this->Employees_db->getBirthdays("MONTH(birthday) >= MONTH('$start') AND MONTH(birthday) <= MONTH('$end')");
		foreach($birthday as $h)
		{
			$dt = array(
				'title' => 'Birthday - '.$h['firstname'].' '.$h['lastname'],
				'start' => mktime(0, 0, 0, $h['month'], $h['day'], date("y",strtotime('now'))),
				'allDay' => true,
				'editable' => false,
				'disableDragging' => true,
				'disableResizing' => true,
				'addClass' => 'birthday',
				'eventID' => $h['id'],
				'type' => 'birthday',
				'isHidden' => false
				);
			if($dt)
				array_push($data, $dt);
		}
		
		//get pending punches
		// $n=1;
		// while($n<=100)
		// {		
		if(!$userid) $userid = $this->tank_auth->get_user_id();
		$cond = array('punchdate >=' => $start, 'punchdate <=' => $end, 'user_id' => $userid, 'punch_alteration_status'=>'PENDING');
		$punchalterations = $this->Punch_alterations_db->getPunchAlterations($cond);
		foreach($punchalterations as $pa)
		{
			$dt = array(
				'title' => ($pa['is_in_or_out']) ? 'In-Pending' : 'Out-Pending',
				'start' => $pa['punchdate'],
				'allDay' => false,
				'editable' => false,
				'disableDragging' => true,
				'disableResizing' => true,
				'addClass' => 'punch-alteration',
				'eventID' => $pa['punch_alteration_id'],
				'type' => 'punch-alteration',
				'isHidden' => false
				);
			if($dt)
				array_push($data, $dt);
		}
		// $n++;
		// }
		
		//get pending leaves			
		if(!$userid) $userid = $this->tank_auth->get_user_id();
		$leaveapplications = $this->Leave_applications_db->getLeaveApplications("leave_from >= '$start' AND leave_to <= '$end' AND  user_id = $userid AND (leave_application_status = 'PENDING' OR leave_application_status = 'APPROVED')");
		foreach($leaveapplications as $la)
		{
			$dt = array(
				'title' => $la['leave_name'].'-Pending',
				'start' => $la['leave_from'],
				'end' => $la['leave_to'],
				'allDay' => true,
				'editable' => false,
				'disableDragging' => true,
				'disableResizing' => true,
				'addClass' => ($la['leave_application_status']=="PENDING") ? 'leave-application-pending' : 'leave-application',
				'eventID' => $la['leave_application_id'],
				'type' => 'leave_application',
				'isHidden' => false
				);
			if($dt)
				array_push($data, $dt);
		}

		
		//get punches		
		if(!$userid) $userid = $this->tank_auth->get_user_id();
		$cond = array('punchdate >=' => $start, 'punchdate <=' => $end, 'user_id' => $userid);
		$events = $this->Punches_db->getPunches($cond);
		$count = 1;
		$pcount = 0;
		foreach($events as $e)
		{			
			$title = ($e['is_in_or_out']) ? 'In' : 'Out';
			$start = $e['punchdate'];
			$end = '';
			$isHidden = false;
			$day = 'groupDay'.date("F",strtotime($e['punchdate'])).'-'.date("d",strtotime($e['punchdate']));
			
			//group records by date
			if($count==1)
				$pday = date("m/d/Y",strtotime($e['punchdate']));
			
			if(date("m/d/Y",strtotime($e['punchdate']))==$pday)
			{
				$pcount++;
				//limit calendar display to 3
				if($pcount>=3)
				{
					$isHidden = true;
					$isViewMore = true;
				}
			}
			else			
				$pcount = 0;
				
			$pday = date("m/d/Y",strtotime($e['punchdate']));
			
			
			//check if record is out, check if next record is same day
			if(!$e['is_in_or_out'])
				$addClass = 'punchout';				
			else
				$addClass = 'punchin';
						
			$dt = array(
				'title' => $title,
				'start' => $start,
				'end' => $end,
				'allDay' => false,
				'editable' => false,
				'disableDragging' => true,
				'disableResizing' => true,
				'addClass' => $addClass,
				'eventID' => $e['punch_id'],
				'type' => 'punch',
				'isHidden' => $isHidden,
				'day' => $day
				);
			if($dt)
				array_push($data, $dt);
			$count++;
		}
		
		
		echo json_encode($data);		
	}
	
	function js() {
        $this->load->view('js/script.js');
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */